﻿// genFIles.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <windows.h>
#include <iostream>
#include <thread>
#include <vector>
#include <memory>
#include <fstream>
#include <string>

char g_buf[1024] = { 0 };
int g_filesize = 1024;

int g_files = 0;
int g_dir = 0;

bool mkpath(std::string pathname)
{
   auto ret = !! ::CreateDirectoryA(pathname.c_str(), NULL);    
   if (ret)
   {
       g_dir++;
    }
   return ret;
}

void writefile(std::string &path)
{
    int n = g_filesize / 1024 + (g_filesize % 1024);

    std::ofstream of;
    of.open(path);
    if (of.is_open())
    {
        for (int i = 0; i < n; ++i)
        {
            auto ts = path + "-" + std::to_string(time(NULL)) + "-" + std::to_string(std::rand() % 100);
            int writen = g_filesize - ts.length();
            memcpy(g_buf, ts.c_str(), ts.length());
            of.write(g_buf, sizeof(g_buf));
        }
        of.close();
        g_files++;

    }
    else
    {
        std::cout << "open file error:" << path << std::endl;
    }
}

void makepathx(std::string &filepath)
{
    mkpath(filepath);
}
void make_file(std::string &filepath, std::string &rootName, int level, int files, int dirs)
{
    if (level == 0)
    {
        return;
    }

    // rootPath2= 20777031/dir10/10-1
    for (int k = 1; k <= files; ++k)
    {
        std::string name = rootName + "-" + std::to_string(k) + ".txt";
        std::string filename = filepath + name;
        writefile(filename);
    }

    for (int j = 1; j <= dirs; ++j)
    {
        std::string subdir = rootName + "_" + std::to_string(j);
        std::string fp = filepath + subdir +"/";
        // rootPath2= 20777031/dir10/dir10_1
        makepathx(fp);
        make_file(fp, subdir, level - 1, j-1, j-1);
    }

    //make_file(filepath, rootName, level - 1, files, dirs);
}

int make_files()
{
    std::string dirName;
    std::string tsTime = std::to_string(GetTickCount64());

    std::cout << "请输入目录名称(Y 默认):" << tsTime << std::endl;

    std::cin >> dirName;
    if (dirName.empty())
    {
        dirName = "test_dir";
    }
    else if ("y" == dirName || "Y" == dirName)
    {
        dirName = tsTime;
    }

    makepathx(dirName);

    std::cout << "请输入根目录(" << dirName << ") 下子目录的数量：" << std::endl;
    int level = 0;
    std::cin >> level;  //10

    std::cout << "请输入文件大小(KB)：输入0默认=1048576=1MB" << std::endl;
    std::cin >> g_filesize;  //10
    if (g_filesize == 0)
    {
        g_filesize = 1048576;
    }
    std::string path = dirName +"/";
    std::string rootName = "dir" + std::to_string(level);

    //std::string rootName = "dir" + std::to_string(level);
    std::string filepath = path + rootName + "/";
    // rootPath2= 20777031/dir10
    makepathx(filepath);

    make_file(filepath, rootName, level, level, level);

    std::cout << "gen files:" << g_files << " dirs:" << g_dir << " path:" << path;
    return 0;
}



#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
        EXTERN_C __declspec(selectany) const type name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}

MIDL_DEFINE_GUID(IID, IID_IMCloudSyncRootShellFolder, 0x3C6C4AE3, 0xCF26, 0x4053, 0x99, 0x36, 0x96, 0xD7, 0xEE, 0x60, 0x2E, 0x5E);


MIDL_DEFINE_GUID(IID, LIBID_MCLOUDSYNCROOTLib, 0x3C6C4AE3, 0x7E36, 0x4E68, 0xB1, 0x25, 0x53, 0xC5, 0x35, 0xBC, 0x4A, 0x9F);


MIDL_DEFINE_GUID(CLSID, CLSID_MCloudSyncRootShellFolder, 0x3C6C4AE3, 0x9AE3, 0x436F, 0xBE, 0x29, 0xE1, 0xC2, 0xF3, 0x8A, 0x5E, 0xEC);

#include <thread>
#include <future>
int main()
{
   /*auto ret = */std::async(std::launch::async, []() {
        printf("run....");
        Sleep(10000);
        printf("end....");
        return 0;
        });

    printf("main....");
    while (1) Sleep(10);
    WCHAR szSyncfolderClassID[MAX_PATH], szSubKey[MAX_PATH], szData[MAX_PATH];
    StringFromGUID2(CLSID_MCloudSyncRootShellFolder, szSyncfolderClassID, ARRAYSIZE(szSyncfolderClassID));
    wprintf(L"%s", szSyncfolderClassID);

    for (int i = 0; i < 1024; ++i)
    {
        g_buf[i] = rand() % 10;
    }
    make_files();

}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
